package com.moli.controller;

import com.moli.domain.ResponseResult;
import com.moli.domain.dto.AddArticleDto;
import com.moli.domain.dto.ArticleDto;
import com.moli.domain.entity.Article;
import com.moli.domain.vo.PageVo;
import com.moli.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author： XC
 * @Date： 2023-06-09 10:15
 * @Description： 博客文章控制器
 */

@RestController
@RequestMapping("/content/article")
public class ArticleController {

	@Autowired
	private ArticleService articleService;

	@GetMapping("/list")
	public ResponseResult list(Article article, Integer pageNum, Integer pageSize) {
		PageVo pageVo = articleService.selectArticlePage(article, pageNum, pageSize);
		return ResponseResult.okResult(pageVo);
	}

	/**
	 * 热门文章列表
	 *
	 * @return
	 */
	@GetMapping("/hotArticleList")
	public ResponseResult hotArticleList() {

		ResponseResult result = articleService.hotArticleList();
		return result;
	}

	/**
	 * 文章列表
	 *
	 * @param pageNum
	 * @param pageSize
	 * @param categoryId
	 * @return
	 */
	@GetMapping("/articleList")
	public ResponseResult articleList(Integer pageNum, Integer pageSize, Long categoryId) {
		return articleService.articleList(pageNum, pageSize, categoryId);
	}

	/**
	 * 更新浏览量
	 *
	 * @param id
	 * @return
	 */
	@PutMapping("/updateViewCount/{id}")
	public ResponseResult updateViewCount(@PathVariable("id") Long id) {
		return articleService.updateViewCount(id);
	}

	/**
	 * 获取文章详细信息
	 *
	 * @param id
	 * @return
	 */
	@GetMapping("/{id}")
	public ResponseResult getArticleDetail(@PathVariable("id") Long id) {
		return articleService.getArticleDetail(id);
	}

	@PutMapping
	public ResponseResult edit(@RequestBody ArticleDto article) {
		articleService.edit(article);
		return ResponseResult.okResult();
	}

	@DeleteMapping("/{id}")
	public ResponseResult delete(@PathVariable Long id) {
		articleService.removeById(id);
		return ResponseResult.okResult();
	}

	@PostMapping
	public ResponseResult add(@RequestBody AddArticleDto article){
		return articleService.add(article);
	}
}
